package cn.icatw.blog.controller;

import cn.icatw.blog.dao.pojo.SysUser;
import cn.icatw.blog.service.SysUserService;
import cn.icatw.blog.vo.PassVo;
import cn.icatw.blog.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author icatw
 * @date 2022/3/2
 * @apiNote
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/currentUser")
    public Result getUserInfo(@RequestHeader("Authorization") String token) {
        return sysUserService.findUserByToken(token);
    }

    @GetMapping("/currentUserInfo")
    public Result getCurrUserInfo(@RequestHeader("Authorization") String token) {
        return sysUserService.findUserInfoByToken(token);
    }

    @PutMapping("/update")
    public Result updateUserInfo(@RequestBody SysUser newUser,
                                 @RequestHeader("Oauth-Token") String token) {
        Result result = sysUserService.findUserInfoByToken(token);
        SysUser user = (SysUser) result.getData();
        newUser.setId(user.getId());
        boolean flag = sysUserService.updateById(newUser);
        if (flag) {
            return Result.success(sysUserService.getById(newUser.getId()));
        }
        return Result.fail(500, "修改失败！");
    }

    @PutMapping("/updatePass")
    public Result updatePass(@RequestBody PassVo passVo) {
        return sysUserService.updatePass(passVo);
    }
}
